package com.zkdcloud.controller;

import com.xdja.interceptor.annotation.BeforeProcess;
import com.xdja.interceptor.annotation.Validate;
import com.xdja.interceptor.bean.ResultBean;
import com.xdja.interceptor.exception.InvokeException;
import com.xdja.interceptor.util.ValidateUtils;
import com.zkdcloud.aspectInterceptor.advice.TokenAdvice;
import com.zkdcloud.aspectInterceptor.validatie.ProductControllerValidate;
import com.zkdcloud.domain.Admin;
import com.zkdcloud.domain.Product;
import com.zkdcloud.dto.Pagination;
import com.zkdcloud.dto.QueryProductBean;
import com.zkdcloud.service.ProductService;
import org.jfaster.mango.plugin.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.HTTP_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.OPERATOR_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.REQUEST_ADMIN;

/**
 * 商品管理
 *
 * @author zk
 * @since 2018/4/18
 */
@RestController
@RequestMapping(value = "/mg/product")
public class ProductController {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(ProductController.class);
    /**
     * productService
     */
    @Autowired
    private ProductService productService;

    /**
     * 获取所有美食列表
     *
     * @return resultBean
     */
    @BeforeProcess(advice = TokenAdvice.class)
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResultBean getList(QueryProductBean queryProductBean, Page page, Map<String,Object> map) {
        List<Product> productList;

        if(StringUtils.isEmpty(queryProductBean.getCategoryId())){
            String bid = ((Admin) map.get(REQUEST_ADMIN)).getBusinessId();
            productList = productService.getAllByBid(bid,page);
        } else {
            productList = productService.getAllByCid(queryProductBean.getCategoryId(),page);
        }

        return ResultBean.successResult(HTTP_SUCCESS, new Pagination(page,productList));
    }

    /**
     * 获取指定美食详情
     *
     * @param id 美食id
     * @return resultBean
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public ResultBean getDetail(@PathVariable("id") String id) {
        return ResultBean.successResult("");
    }

    /**
     * 更新指定美食
     *
     * @param product 美食
     * @return resultBean
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultBean update(Product product) throws InvokeException {
        ValidateUtils.assertCondition(!StringUtils.isEmpty(product.getId()), "id is not null");
        productService.update(product);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }

    /**
     * 移除指定美食
     *
     * @param id 美食id
     * @return resultBean
     */
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public ResultBean remove(@PathVariable("id") String id) {
        productService.remove(id);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }

    /**
     * 添加美食
     *
     * @param product product
     * @return resultBean
     */
    @BeforeProcess(advice = TokenAdvice.class, validate = @Validate(value = ProductControllerValidate.class, method = "add"))
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultBean add(Product product) {
        productService.add(product);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }
}
